$(function () {
  let $tel = $('input[name=tel]');
  let $sms = $('input[name=sms-captcha]');
  let $sms_btn = $('.sms-captcha-btn');
  let $psd = $('input[name=password]');
  let $psd_confirm = $('input[name=password-confirm]');
  let $submit_btn = $('.form-contain');
  let sms_return_status = "";

  //未发送验证码前不允许输入密码
  $psd.attr('disabled', true);
  $psd_confirm.attr('disabled', true);

  //获取验证码绑定事件，向后端发起请求验证码请求
  $sms_btn.click(get_sms);
  //重置密码提交绑定事件，像后端发起请求
  $submit_btn.submit(check_submit);

  //获取短信验证码函数
  function get_sms() {
    let tel = $tel.val();
    //若手机号格式输入有误
    if (!(/^1[3-9]\d{9}$/).test(tel)) {
      message.showError("手机号码有误，请检查");
      return
    }
    //手机号初步校验之后通过后发起ajax请求
    $.ajax({
      url: '/sms/retrieve/',
      method: 'POST',
      data: {
        'tel': tel
      },
      type: 'json',
      async: false
    })

        .done(function (res) {
          if (res.no === "0") {
            message.showSuccess(res.msg);
            sms_return_status = "success";
            //验证码发送成功后，去掉密码输入框的不可输入属性
            $psd.attr('disabled', false);
            $psd_confirm.attr('disabled', false);

            ~function () {
              //短信验证码发送成功后 获取验证码标签改变内容,色湖之60s的倒计时
              let t = 60;
              let timer = setInterval(function () {
                if (t === 1) {
                  clearInterval(timer);
                  $sms_btn.html('获取短信验证码');
                  $sms_btn.removeAttr('style');
                  $sms_btn.removeAttr('disabled');
                  $sms_btn.bind('click', get_sms);
                } else {
                  $sms_btn.html(`${t}s`);
                  t -= 1;
                  //按钮变灰
                  $sms_btn.attr('style', 'opacity: 0.2');
                  //按钮不可再点击
                  $sms_btn.attr('disabled', true);
                  //按钮除去点击事件，避免在此期间发送ajax请求
                  $sms_btn.unbind('click', get_sms)
                }
              }, 1000);
            }();
          } else {
            message.showError(res.msg)
          }
        })
        .fail(() => {
          message.showError("服务器异常")
        })
  }

  //重置密码函数，提交新密码和短信验证码到后端
  function check_submit(ev) {
    ev.preventDefault();
    let tel = $tel.val();
    let sms_code = $sms.val();
    let password = $psd.val();
    let password_confirm = $psd_confirm.val();
    if (sms_return_status === "") {
      message.showError("请先获取短信验证码");
      return
    }
    if (!tel || !(/^1[3-9]\d{9}$/).test(tel)) {
      message.showError("无效的手机号码，请检查");
      return
    }
    if (!sms_code || !(/^\d{6}$/).test(sms_code)) {
      message.showError("验证码不正确");
      return
    }
    if (!(/^\w{6,20}$/).test(password) || !(/^\w{6,20}$/).test(password_confirm)) {
      message.showError("密码必须是6-20位的数字字母组合");
      return
    }
    if (password && password_confirm && (password !== password_confirm)) {
      message.showError("两次密码不一致");
      return
    }

    let info = {
      'tel': tel,
      'sms_code': sms_code,
      'password': password,
      'password_confirm': password_confirm
    };
    $.ajax({
      url: '/user/retrieve/',
      method: 'POST',
      data: JSON.stringify(info),
      contentType: 'application/json;charset=utf-8',
      dataType: 'json',
      async: false
    })
        .done(function (res) {
          console.log(res);
          if (res.no === "0") {
            message.showSuccess("密码重置成功，即将跳转至登录页");
            setTimeout(function () {
              window.location.href = '/user/login/'
            }, 1500);
          } else {
            message.showError(res.msg);
          }
        })
        .fail(function (res) {
          console.log(res);
          message.showError("服务器异常")
        })
  }

  csrf()

});
